package cn.jly.boot.mp.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @PackageName cn.jly.boot.mp.controller
 * @ClassName SpringSessionController
 * @Description 测试spring session
 * @Author 姬岚洋
 * @Date 2021/1/5 下午 2:58
 */
@RestController
public class SpringSessionController {

    @GetMapping("/setSession")
    public Map<String, Object> setSession(HttpServletRequest request) {
        final HashMap<String, Object> map = new HashMap<>();

        // 存入session
        request.getSession().setAttribute("message", request.getRequestURL());
        // 存入map
        map.put("requestUrl", request.getRequestURL());

        return map;
    }

    /**
     * java -jar app.jar --server.port=8080
     * java -jar app.jar --server.port=8081
     *
     * @param request
     * @return
     */
    @GetMapping("/getSession")
    public Map<String, Object> getSession(HttpServletRequest request) {
        final HashMap<String, Object> map = new HashMap<>();

        // 取session
        map.put("sessionId", request.getSession().getId());
        map.put("message", request.getSession().getAttribute("message"));

        return map;
    }
}
